package com.gx.smart.construction.ui.common.db;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.blankj.utilcode.util.Utils;
import com.gx.smart.construction.ui.house.db.RoomInfoDao;
import com.gx.smart.construction.ui.house.model.RoomInfo;

/**
 * @author: xiaosy
 * 时间: 2022/2/11
 * @Description: 这个app使用的数据库
 * @Version: 1.0
 **/
@Database(entities = {RoomInfo.class}, version = 2, exportSchema = true)
public abstract class ConstructionDatabase extends RoomDatabase {
    private static final String DATABASE_NAME = "construction_db";
    private static ConstructionDatabase sInstance;

    public static ConstructionDatabase getInstance() {
        if (sInstance == null) {
            synchronized (ConstructionDatabase.class) {
                if (sInstance == null) {
                    sInstance = Room
                            .databaseBuilder(Utils.getApp(), ConstructionDatabase.class, DATABASE_NAME)
                            .fallbackToDestructiveMigration()
                            .addMigrations(MIGRATION_1_2)
                            .build();
                }
            }
        }
        return sInstance;
    }

    static final Migration MIGRATION_1_2 = new Migration(1, 2)
    {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database)
        {
            //do something
            database.execSQL("ALTER TABLE room_info "
                    + " ADD COLUMN page INTEGER NOT NULL DEFAULT 1");
        }
    };

    /**
     * 调用的接口
     * @return
     */
    public abstract RoomInfoDao getRoomInfoDao();
}
